package net.wanho.controller;

import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.ChineseCaptcha;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import lombok.RequiredArgsConstructor;
import net.wanho.entity.User;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Author：汤小洋
 * Date：2025-09-23 9:30
 * Description：<描述>
 */
@RestController
@RequiredArgsConstructor
public class HelloController {

    private final StringRedisTemplate stringRedisTemplate;
    private final RedisTemplate<String, Object> redisTemplate;

    @RequestMapping("/test1")
    public String test1(){
        // redisTemplate.opsForValue().set("name", "帅哥");
        // Boolean isSuccess = redisTemplate.opsForValue().setIfAbsent("name", "张三");// 相当于setnx
        // System.out.println(isSuccess);
        //
        // redisTemplate.opsForValue().set("address", "南京", 2, TimeUnit.HOURS); // 过期时间
        //
        // redisTemplate.delete("aaa"); // 删除
        //
        // Set<String> keys = redisTemplate.keys("n*"); // 获取key
        // System.out.println(keys);
        //
        // String str = redisTemplate.opsForValue().get("name");

        User user = new User(1, "张三", 18, "男");
        redisTemplate.opsForValue().set("user", user);

        User u = (User) redisTemplate.opsForValue().get("user");
        System.out.println(u);

        return "success";
    }

    @RequestMapping("/captchaImage")
    public String test2(){
        // Captcha captcha = new SpecCaptcha();
        Captcha captcha = new ArithmeticCaptcha();
        // Captcha captcha = new GifCaptcha();
        // Captcha captcha = new ChineseCaptcha();

        String img = captcha.toBase64(); // 验证码图片的Base64格式
        String text = captcha.text();

        // 将验证码保存到Redis中
        stringRedisTemplate.opsForValue().set("captcha", text, 2, TimeUnit.MINUTES);

        String trueCode = stringRedisTemplate.opsForValue().get("captcha");
        System.out.println(trueCode);

        return img;
    }

}
